package se.his.assignment1.client;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

public class Client implements ActionListener {

    private String m_name = null;
    private final ChatGUI m_GUI;
    private ServerConnection m_connection = null;

    public static void main(String[] args) {
        if (args.length < 3) {
            System.err.println("Usage: java Client serverhostname serverportnumber username");
            System.exit(-1);
        }

        try {
            Client instance = new Client(args[2]);
            instance.connectToServer(args[0], Integer.parseInt(args[1]));
        } catch (NumberFormatException e) {
            System.err.println("Error: port number must be an integer.");
            System.exit(-1);
        }
    }

    private Client(String userName) {
        m_name = userName;

        // Start up GUI (runs in its own thread)
        m_GUI = new ChatGUI(this, m_name);
    }

    private void connectToServer(String hostName, int port) {
        //Create a new server connection
        m_connection = new ServerConnection(hostName, port);
        if (m_connection.handshake(m_name)) {
            listenForServerMessages();
        } else {
            System.err.println("Unable to connect to server");
        }
    }

    private void listenForServerMessages() {
        do {
            m_GUI.displayMessage(m_connection.receiveChatMessage());
        } while (true);
    }

    // Sole ActionListener method; acts as a callback from GUI when user hits enter in input field
    public void actionPerformed(ActionEvent e) {
        // Since the only possible event is a carriage return in the text input field,
        // the text in the chat input field can now be sent to the server.
        m_connection.sendChatMessage(m_GUI.getInput());
        m_GUI.clearInput();
    }
}


class ChatGUI extends JFrame implements ActionListener {
    // This class should be sufficiently complete, although
    // you are allowed to change it if you wish.

    private final JTextField m_chatInput;
    private final JTextArea m_chatOutput;

    public ChatGUI(ActionListener listener, String userName) {
        setSize(700, 500);
        setTitle("Chat client for " + userName);

        m_chatOutput = new JTextArea(10, 15);
        m_chatInput = new JTextField(20);

        m_chatOutput.setWrapStyleWord(true);
        m_chatOutput.setLineWrap(true);
        m_chatOutput.setEditable(false);
        m_chatOutput.setBackground(Color.BLACK);
        m_chatOutput.setForeground(Color.GREEN);

        Container pane = getContentPane();
        pane.add(m_chatOutput, BorderLayout.NORTH);
        pane.add(m_chatInput, BorderLayout.SOUTH);
        pane.add(new JScrollPane(m_chatOutput), BorderLayout.CENTER);

        m_chatInput.addActionListener(listener);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public void displayMessage(String message) {
        m_chatOutput.append(message + "\n");
    }

    public String getInput() {
        return m_chatInput.getText();
    }

    public void clearInput() {
        m_chatInput.setText("");
    }

    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
    }
}


